package com.demo.accessibility.utils

import android.content.Context
import android.content.Context.POWER_SERVICE
import android.content.Intent
import android.net.Uri
import android.os.Build.VERSION
import android.os.PowerManager
import com.blankj.utilcode.util.LogUtils


object PermissionUtils {
    /**
     * 忽略电池优化
     */
    fun ignoredBatteryOptimization(
        context: Context,
        callback: ((result: String?) -> Unit)? = null
    ) {
        if (VERSION.SDK_INT >= 23) {
            try {
                val powerManager = context.getSystemService(POWER_SERVICE) as PowerManager
                val hasIgnored = powerManager.isIgnoringBatteryOptimizations(context.packageName)
                if (hasIgnored) {
                    callback?.invoke("hasIgnored")
                    return
                }
                val intent = Intent("android.settings.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS")
                val stringBuilder = StringBuilder()
                stringBuilder.append("package:")
                stringBuilder.append(context.packageName)
                intent.data = Uri.parse(stringBuilder.toString())
                context.startActivity(intent)
            } catch (e: Exception) {
                e.printStackTrace()
                val objArr = arrayOfNulls<Any>(1)
                val stringBuilder2 = StringBuilder()
                stringBuilder2.append("requestIgnoreBatteryOptimizations: ")
                stringBuilder2.append(e.message)
                objArr[0] = stringBuilder2.toString()
                LogUtils.e(*objArr)
            }
        }
    }


}